﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Serialization;

namespace ArgusLight.Fractals2D.ComplexFractalColoring.IterationBasedSinus
{
	[XmlType(TypeName="ComplexIterationBasedSinusColoring")]
	public abstract class Coloring : ComplexFractalColoring.PeriodicColoring
	{
		public override void Reset(FractalParameter fParameter)
		{
			base.Reset(fParameter);
			this.Data = new ColoringData(fParameter.PixelWidth, fParameter.PixelHeight);
		}

		public override void PostCalculation()
		{
			base.PostCalculation();
			for (int x = 0; x < this.fractalParameter.PixelWidth; x++)
			{
				for (int y = 0; y < this.fractalParameter.PixelHeight; y++)
				{
					int i = this.data.iterations[x, y];
					if (i < this.data.ArgumentInterval.Min && i > 0)
						this.data.ArgumentInterval.Min = i;
					if (i > this.data.ArgumentInterval.Max && i < this.fractalParameter.Iterations)
						this.data.ArgumentInterval.Max = i;
				}
			}
		}

		protected override double GetFrequency()
		{
			return 2 * this.parameter.NumberOfColorPeriods * System.Math.PI / this.data.ArgumentInterval.Length;
		}

		private const Double p2 = 2 * System.Math.PI / 3;
		private const Double p3 = 4 * System.Math.PI / 3;

		public static byte[] GetColorBytes_Sinus2(Int32 i, Int32 MaxI, Double Frequency, Double verschiebung)
		{
			if (i >= MaxI || i < 1)
				return new byte[] { 0, 0 };

			byte a, b;
			a = b = 0;

			verschiebung *= 2 * Math.PI;

			a = (byte)((System.Math.Sin(i * Frequency + verschiebung) + 1) * 127);
			b = (byte)((System.Math.Sin(i * Frequency + p2 + verschiebung) + 1) * 127);

			return new byte[] { a, b };
		}

		public static byte[] GetColorBytes_Sinus3(Int32 i, Int32 MaxI, Double Frequency, Double verschiebung)
		{
			if (i >= MaxI || i < 1)
				return new byte[] { 0, 0, 0 };

			byte a, b, c;
			a = b = c = 0;

			verschiebung *= 2 * Math.PI;

			a = (byte)((System.Math.Sin(i * Frequency + verschiebung) + 1) * 127);
			b = (byte)((System.Math.Sin(i * Frequency + p2 + verschiebung) + 1) * 127);
			c = (byte)((System.Math.Sin(i * Frequency + p3 + verschiebung) + 1) * 127);

			return new byte[] { a, b, c };
		}
	}
}
